<template>
  <view class="wrap">
    <view class="status"></view>
    <view class="list">
      <uv-cell-group>
        <uv-cell title="当前版本" :value='appVersion' center border titleStyle='font-weight: 500; font-size: 32rpx; color: #2B2B2B;' cellStyle='background-color: #FFF'></uv-cell>
        <uv-cell title="清理缓存" :value='cacheSize' center isLink border titleStyle='font-weight: 500; font-size: 32rpx; color: #2B2B2B;' cellStyle='background-color: #FFF' @click="clearCache"></uv-cell>
        <uv-cell title="消息推送" center isLink border titleStyle='font-weight: 500; font-size: 32rpx; color: #2B2B2B;' cellStyle='background-color: #FFF'>
          <template v-slot:value>
            <uv-switch v-model="is_push" activeColor='#E43222' :activeValue='1' :inactiveValue='2' :loading='loading' @change='change'></uv-switch>
          </template>
        </uv-cell>
      </uv-cell-group>
    </view>
    <view class="list">
      <uv-cell-group>
        <uv-cell title="注销账户" url="/pages/mine/setting/phone" center isLink border titleStyle='font-weight: 500; font-size: 32rpx; color: #2B2B2B;' cellStyle='background-color: #FFF'></uv-cell>
      </uv-cell-group>
    </view>
  </view>
</template>

<script setup>
import { onLoad } from "@dcloudio/uni-app";
import { toast, getUser } from '@/common';
import { ref } from "vue";
import { post } from '@/api';
import { useUser } from '@/store';

let userInfo = uni.$uv.deepClone(useUser().userInfo);
console.log(userInfo.is_push);
let cacheSize = ref(0 + 'B');
let appVersion = ref('');
let is_push = ref(userInfo.is_push == 1 ? 1 : 2);
let loading = ref(false);

onLoad(() => {
  // #ifdef APP
  appVersion = plus.runtime.version; 
  getAppInfo();
  // #endif
})

function change(e){
  const params = {
    is_push: is_push.value
  }
  post('api/user/profile', params).then(res => {
    if(res.code == 1){
      toast('修改成功');
      getUser();
    }
  })
}
// 获取缓存大小
function getAppInfo(){
  plus.cache.calculate(size => {
    if (size < 1024) {
      cacheSize.value = size + 'B';
    } else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
      cacheSize.value = Math.floor((size / 1024) * 100) / 100 + 'KB';
    } else if (size / 1024 / 1024 >= 1) {
      cacheSize.value = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
    }
  });
}
//清除缓存
function clearCache() {
  // #ifdef H5
  toast('请在APP端操作');
  return;
  // #endif
  let os = plus.os.name;
  if (os == 'Android') {
    let main = plus.android.runtimeMainActivity();
    let sdRoot = main.getCacheDir();
    let files = plus.android.invoke(sdRoot, "listFiles");
    let len = files.length;
    for (let i = 0; i < len; i++) {
      let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
      plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
        if (entry.isDirectory) {
          entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  	
            getAppInfo(); // 重新计算缓存
          }, function(e) {
            console.log(e.message)
          });
        } else {
          entry.remove();
        }
      }, function(e) {
        console.log('文件路径读取失败')
      });
    }
    toast('缓存清理完成')

  } else { // ios  
    plus.cache.clear(function() {
      toast('缓存清理完成')
      getAppInfo();
    });
  }
}
</script>

<style lang="scss" scoped>
  .list{
    padding: 0 30rpx;
    margin-bottom: 20rpx;
    background-color: #fff;
    :deep(.uv-cell-group){
      .uv-cell__body.uv-cell--center{
        padding: 20rpx 0;
        .uv-cell__left-icon-wrap{
          display: none;
        }
      }
    }
    :deep(.uv-cell__value){
      font-weight: 400;
      font-size: 28rpx;
      color: #999999;
    }
  }
  .status{
    width: 100%;
    height: 20rpx;
    background-color: #F5F5F5;
  }
  .wrap{
    width: 100%;
    min-height: 100vh;
    background-color: #F5F5F5;
  }
</style>